﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Services_getDistrictList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

        string DistId = Request.QueryString[0].ToString().Trim();
        
        //查询地区的经纬度
        string queryCoordinate = "select LAT,LNG,ROOTID from china_district where distid = '" + DistId + "'";
        DataSet dsCoo = AccessDatabase.Query(queryCoordinate);
        DataTable dtCoo = dsCoo.Tables[0];
        string LAT = dtCoo.Rows[0][0].ToString();
        string LNG = dtCoo.Rows[0][1].ToString();

        if (LAT == "")
        {
            string queryString = "select LAT,LNG from china_district where distid='" + dtCoo.Rows[0][2].ToString() + "'";
            DataSet ds2 = AccessDatabase.Query(queryString);
            DataTable dt2 = ds2.Tables[0];
            if (dt2.Rows.Count > 0)
            {
                LAT = dt2.Rows[0][0].ToString();
                LNG = dt2.Rows[0][1].ToString();
            }
            if (LAT == "")
            {
                //默认坐标是北京
                LAT = "39.92";
                LNG = "116.46";
            }

        }
        
        string result = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        result += "<Districts>" + "\n";
        try
        {
            string queryString = "select distid,distname from china_district where rootid = '" + DistId + "'";
            DataSet ds = AccessDatabase.Query(queryString);
            DataTable dt = ds.Tables[0];
           

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    result += "<District id = '" + dt.Rows[i][0].ToString() + "' name = '" + dt.Rows[i][1].ToString() + "' lat = '" + LAT + "' lng = '" + LNG + "' />" + "\n";
                }
            }
            else
            {
                result += "<District id = 'Error' name = '没有该行政区' lat='" + LAT + "' lng='" + LNG + "'/>" + "\n";
            }
        }
        catch (Exception ex)
        {
            result += "<District id = 'Error' name = '" + ex.Message.ToString().Substring(0, 50) + "...' />" + "\n";
        }
        result += "</Districts>" + "\n";
        Response.Write(result);


    }
}
